<?php

/**
 * First basic plugin
 *
 * @version $Id$
 * @copyright 2010
 */

class Uploadify extends Plugin
{
	public $plugin_name = "Uploadify"; //name of plugin
	public $plugin_short_desc = "Multiple jQuery flash upload"; //short description of plugin
	public $plugin_long_desc = "Uploads multiple files through flash and jQuery library"; //longer description - what does plugin do
	public $version = "1.0"; //version of plugin
	public $has_sidebar = false; //does this plugin have sidebar -> function generate_sidebar()
	public $has_footer = false; //does this plugin have footer -> function generate_footer()
	public $needs_install = true;
	public $upload_dir = UPLOADS_DIR;
	public $gallery_section = 0; //default section for images from Gallery plugin

	/**
	 * Constructor
	 */
	function __construct()
	{
		global $theme,$core;
		$theme->include_jquery();
		$theme->include_js($this->get_plugin_path().'files/swfobject.js');
		$theme->include_js($this->get_plugin_path().'files/jquery.uploadify.v2.1.0.min.js');
		$theme->include_css($this->get_plugin_path().'files/uploadify.css');

		if($core->get('del')) //wanna delete file?
		{
			$file = mysql_fetch_array(mysql_query("SELECT * FROM ".DB_PREFIX."yaps_uploads WHERE id = ".$core->get('del')));

			$unlink = unlink(UPLOADS_DIR.$file['name']);

			if($unlink)
				$query = mysql_query("DELETE FROM ".DB_PREFIX."yaps_uploads WHERE id = ".$core->get('del'));
		}
	}

	/**
	 * Uploadify::create_upload()
	 * This will create upload button with all javascript etc.
	 * @return
	 */
	public function create_upload()
	{
		global $theme, $core;

		$theme->js("$(document).ready(function() {
	$(\"#uploadify\").uploadify({
		'uploader'       : '".PLUGINS_DIR."uploadify/files/uploadify.swf',
		'script'         : '".PLUGINS_DIR."uploadify/files/upload-file.php?gallery_section=".$this->gallery_section."',
		'cancelImg'      : 'cancel.png',
		'folder'         : '".$this->upload_dir."',
		'queueID'        : 'fileQueue',
		'auto'           : true,
		'multi'          : true,
		'simUploadLimit' : 3
	});
});");

		echo '<div id="fileQueue"></div>
					<input type="file" name="uploadify" id="uploadify" />';
	}

	/**
	 * Uploadify::admin_settings()
	 * Example of usage in Yaps!
	 * @return
	 */
	public function admin_settings()
	{
		global $core;

		echo '<p>Usage in Yaps!:</p>';

		if($core->is_plugin('highlight'))
		{
			$hl = $core->load_plugin('highlight');
			echo $hl->text('$plugin = $core->load_plugin(\'uploadify\');
$plugin->create_upload(); //creates upload button
$plugin->print_file_list(); //prints list of uploaded files');
		}
		else
		{
			echo '$plugin = $core->load_plugin(\'uploadify\');
		$plugin->create_upload(); //creates upload button
		$plugin->print_file_list(); //prints list of uploaded files';
		}

		echo 'Upload a file:';
		$this->create_upload();

		echo '<p>Already uploaded files:</p>';

		$this->print_file_list(true);
	}

	/**
	 * Uploadify::print_file_list()
	 * Print list of uploaded files from DB
	 * @return
	 */
	public function print_file_list($deletable = false)
	{
		global $theme;

		$query = mysql_query("SELECT * FROM ".DB_PREFIX."yaps_uploads");
		if(mysql_num_rows($query) > 0)
		{
			$theme->js('function show_detail(id,event)
{
	$("#i" + id).toggle();
}',false);

			echo '<ul class="upload_list">';

			while($file = mysql_fetch_array($query))
			{
				if(!$deletable) echo '<li>
															<a href="#" onClick="show_detail(\'',$file['id'],'\')">',$theme->icon('magnifier.png'),'</a>
															<a href="',WEB_URL,UPLOADS_DIR,$file['name'],'" target="_blank" class="file_link">',$file['name'],'</a>
															</li>';
				else echo '<li>
									<a href="admin-pluginsettings?plugin=uploadify&amp;del=',$file['id'],'">',$theme->icon('cross.png'),'</a>
									<a href="javascript:show_detail(\'',$file['id'],'\')">',$theme->icon('magnifier.png'),'</a>
									<a href="',WEB_URL,UPLOADS_DIR,$file['name'],'" target="_blank" class="file_link">',$file['name'],'</a>
									</li>';

				echo '<li style="display: none;" id="i',$file['id'],'">',filesize(UPLOADS_DIR.$file['name']),'b |',filetype(UPLOADS_DIR.$file['name']),'</li>';

			}

			echo '</ul>';
		}
	}

	/**
	 * Uploadify::install()
	 * Install function
	 * @return
	 */
	public function install()
	{
		$create_table = mysql_query("CREATE TABLE IF NOT EXISTS `".DB_PREFIX."yaps_uploads` (
		   `id` int(11) NOT NULL AUTO_INCREMENT,
		   `name` text COLLATE utf8_czech_ci NOT NULL,
		   `inserted` datetime NOT NULL,
		   PRIMARY KEY (`id`)
		   ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1");

		$insert_data = mysql_query("INSERT INTO ".DB_PREFIX."yaps_config VALUES('plugin_uploadify_installed','true')");

		if($create_table && $insert_data) return true;
		else return false;
	}
}


?>